package com.example.index.arch.mvvm

import android.app.Activity
import android.app.Application
import android.os.Bundle
import com.example.index.App
import dagger.android.AndroidInjection

object AppInjector {


    fun initialize(app: App) {

        app.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
            override fun onActivityPaused(activity: Activity) {

            }

            override fun onActivityStarted(activity: Activity) {

            }

            override fun onActivityDestroyed(activity: Activity) {

            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {

            }

            override fun onActivityStopped(activity: Activity) {

            }

            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                doInjectorWork(activity)
            }

            override fun onActivityResumed(activity: Activity) {

            }

        })
    }

    private fun doInjectorWork(activity: Activity) {
        if (activity is ActivityInjectable) {
            AndroidInjection.inject(activity)
        }
    }
}